/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package textfilewriter;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Collection;
import org.apache.commons.io.IOUtils;

/**
 *
 * @author GiangLT
 */
public final class AppendUtils {

    public static void appendToFile(final File f, final InputStream in) throws IOException {
        OutputStream stream = null;
        try {
            stream = outStream(f);
            IOUtils.copy(in, stream);
        } finally {
            IOUtils.closeQuietly(stream);
        }
    }

    public static void appendToFile(final File file, final String in) throws IOException {
        InputStream stream = null;
        try {
            stream = IOUtils.toInputStream(in);
            appendToFile(file, stream);
        } finally {
            IOUtils.closeQuietly(stream);
        }
    }

    public static void appendToFile(final File file, final Collection<String> lines) throws IOException {
        for (String line : lines) {
            appendToFile(file, line);
            appendToFile(file, System.getProperty("line.separator"));
        }
    }

    private static OutputStream outStream(final File f) throws IOException {
        return new BufferedOutputStream(new FileOutputStream(f, true));
    }

    private AppendUtils() {
    }
}
